﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Entity;
using DAL;

namespace Project_V1
{
    public partial class FrmAdmin : Form
    {
        public FrmAdmin()
        {
            InitializeComponent();
        }

        #region 窗体移动
        private Point mouseOff;
        private bool leftFlag;
        private void Frm_MouseDown(object sender,MouseEventArgs e)
        {
            if (e.Button==MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y);
                leftFlag = true;
            }
        }
        private void Frm_MouseMove(object sender,MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);
                Location = mouseSet;
            }
        }
        private void Frm_MouseUp(object sender,MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;
            }
        }
        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //登录按钮,输入验证
        private void btnLoginSys_Click(object sender, EventArgs e)
        {
            if (this.txtLoginAccount.Text.Trim().Length==0)
            {
                MessageBox.Show("请输入登录账号!","提示信息");
                this.txtLoginAccount.Focus();//光标闪烁
                return;//下面的代码不执行
            }
            if (this.txtLoginPwd.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入登录密码!", "提示信息");
                this.txtLoginPwd.Focus();//光标闪烁
                return;//下面的代码不执行
            }
            //封装对象
            SysAdmins objAdmins = new SysAdmins()
            {
                LoginId = Convert.ToInt32(this.txtLoginAccount.Text.Trim()),
                LoginPwd = this.txtLoginPwd.Text.Trim()
            };

            if (AdminLogin(objAdmins) == null)
            {
                MessageBox.Show("用户名或密码错误！", "登录提示");
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                //CommonMethods.objAdmins = objAdmins;
            }
        }

        /// <summary>
        /// 登录判断方法
        /// </summary>
        /// <param name="objAdmin"></param>
        /// <returns></returns>
        private SysAdmins AdminLogin(SysAdmins objAdmin)
        {
            string sql = "Select LoginName,Role from sysAdmins where LoginId={0} and LoginPwd='{1}'";
            sql = string.Format(sql, objAdmin.LoginId, objAdmin.LoginPwd);
            DataSet ds = SQLHelper.GetDataSet(sql);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                objAdmin.LoginName = ds.Tables[0].Rows[0]["LoginName"].ToString();
                objAdmin.Role = Convert.ToInt32(ds.Tables[0].Rows[0]["Role"]);
                return objAdmin;
            }
            else
            {
                return null;
            }
        }

        private void txt_UserId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnLoginSys_Click(null, null);
            }
        }
        private void txt_Pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnLoginSys_Click(null, null);
            }
        }

        private void FrmAdmin_Load(object sender, EventArgs e)
        {

        }
    }
}
